#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string>
#include <cstring>

int main()
{
    int fd = open("./bite.txt", O_CREAT | O_TRUNC | O_RDWR, 0644);
    if(fd < 0)
    {
        std::cerr << "open fail!" << std::endl;
        exit(-1);
    }

    const char* buffer = "i like linux!";
    ssize_t n = write(fd, buffer, strlen(buffer));
    if(n < 0)
    {
        std::cerr << "write fail!" << std::endl;
        exit(-2);
    }

    /* 将文件指针置到开头 */
    lseek(fd, 0, SEEK_SET);

    char word[1024] = {0};
    ssize_t m = read(fd, word, sizeof(word) - 1);
    if(m < 0)
    {
        std::cerr << "read fail!" << std::endl;
        exit(-3);
    }

    std::cout << word << std::endl;

    close(fd);
    return 0;
}